package com.example.evanzeng.viewpagertest.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.view.PagerAdapter;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.example.evanzeng.viewpagertest.R;
import com.example.evanzeng.viewpagertest.util.ImageUtil;

import java.util.List;

import jp.wasabeef.glide.transformations.BlurTransformation;


/**
 * Created Date 2019/6/19
 *
 * @author liangkun
 * description：大图浏览适配器
 */
public class CustomPagerAdapter extends PagerAdapter {
    //上下文
    private Context mContext;
    //数据
    private List<Integer> mData;

    /**
     * 构造函数
     * 初始化上下文和数据
     *
     * @param context
     * @param list
     */
    public CustomPagerAdapter(Context context, List<Integer> list) {
        mContext = context;
        mData = list;

    }

    /**
     * 返回要滑动的VIew的个数
     *
     * @return
     */
    @Override
    public int getCount() {
        return mData.size();
    }

    /**
     * 1.将当前视图添加到container中
     * 2.返回当前View
     *
     * @param container
     * @param position
     * @return
     */
    @Override
    public Object instantiateItem(ViewGroup container, final int position) {
        View view = View.inflate(mContext, R.layout.item_large_image, null);
         ImageView ivShow = (ImageView) view.findViewById(R.id.iv_show);
//        final ImageView ivBlur = (ImageView) view.findViewById(R.id.iv_blur);
        ivShow.setImageResource(mData.get(position));
//        if(imageCallback!=null){
//            imageCallback.getImage(mData.get(position),ivShow);
//        }
        container.addView(view);
        return view;
    }


    /**
     * 从当前container中删除指定位置（position）的View
     *
     * @param container
     * @param position
     * @param object
     */
    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // super.destroyItem(container,position,object); 这一句要删除，否则报错
        container.removeView((View) object);
    }

    /**
     * 确定页视图是否与特定键对象关联
     *
     * @param view
     * @param object
     * @return
     */
    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    //    /**
//     * 解析图片
//     *
//     * @param url       地址
//     * @param imageView 图片
//     */
//    private void loadUrl(final String url, final ImageView imageView) {
//
//        Glide.with(mContext)
//                .load(url)
//                .asBitmap()
//                .placeholder(R.mipmap.iv_default_pic)
//                .error(android.R.drawable.ic_menu_report_image).listener(new RequestListener<String, Bitmap>() {
//            @Override
//            public boolean onException(Exception e, String model, Target<Bitmap> target, boolean isFirstResource) {
//                Glide.with(mContext)
//                        .load(url)
//                        .asBitmap()
//                        .placeholder(R.mipmap.iv_default_pic)
//                        .into(imageView);
//                return false;
//            }
//
//            @Override
//            public boolean onResourceReady(Bitmap resource, String model, Target<Bitmap> target, boolean isFromMemoryCache, boolean isFirstResource) {
//                return false;
//            }
//        }).into(imageView);
//    }
    public ImageCallback imageCallback;

    public interface ImageCallback {
        void getImage(Integer resouce,ImageView ivShow);
    }

    public void setImageShow(ImageCallback imageCallback) {
        this.imageCallback = imageCallback;

    }
}
